package com.rul.mars.order.controller;

import com.rul.mars.common.api.CommonPage;
import com.rul.mars.common.api.CommonResult;
import com.rul.mars.mbg.domain.CommentDetail;
import com.rul.mars.mbg.domain.CommentParam;
import com.rul.mars.order.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单评论服务接口
 *
 * @author RuL
 */
@Api(tags = "CommentController", description = "订单评论服务用户接口")
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 用户评论订单
     *
     * @param param 评论参数
     * @return 评论详情
     */
    @ApiOperation("用户评论订单")
    @PostMapping("/evaluate")
    public CommonResult<CommentDetail> commentOrder(@RequestBody CommentParam param) {
        CommentDetail detail = commentService.create(param);
        return CommonResult.success(detail);
    }

    /**
     * 用户删除评论
     *
     * @param id 用户ID
     * @return 删除结果
     */
    @ApiOperation("删除评论")
    @PostMapping("/delete")
    public CommonResult deleteComment(@RequestParam("id") Long id) {
        boolean success = commentService.deleteComment(id);
        if (success) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    /**
     * 查看评论详情
     *
     * @param id 评论ID
     * @return 评论详情
     */
    @ApiOperation("查看评论详情")
    @GetMapping("/detail/{id}")
    public CommonResult<CommentDetail> detail(@PathVariable("id") Long id) {
        CommentDetail detail = commentService.detail(id);
        return CommonResult.success(detail);
    }


    /**
     * 分页查询商品评论
     *
     * @param productId 商品ID
     * @param pageNum   分页号
     * @param pageSize  分页大小
     * @return 商品评论信息
     */
    @ApiOperation("分页查询商品评论")
    @GetMapping("/list")
    public CommonResult<CommonPage<CommentDetail>> listComment(@RequestParam Long productId,
                                                               @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                               @RequestParam(required = false, defaultValue = "20") Integer pageSize) {
        List<CommentDetail> detailList = commentService.listCommentDetail(productId, pageNum, pageSize);
        CommonPage<CommentDetail> page = CommonPage.restPage(detailList);
        return CommonResult.success(page);
    }
}
